package com.cjm.algorithm.dfs;

public class Ti463 {
    public static void main(String[] args) {

    }
    int sum = 0;
    int xx = 0;
    int yy = 0;
    public int islandPerimeter(int[][] grid) {
        sum = 0;
        yy = grid[0].length;
        xx = grid.length;
        for (int i = 0; i < xx; i++) {
            for (int k = 0; k < yy; k++) {
                int unknow = grid[i][k];
                if (unknow == 0 || unknow == 2) {
                    continue;
                }
                hello463(grid, i, k);
            }
        }
        return sum;
    }

    private void hello463(int[][] grid, int x, int y) {
        if (x < 0 || x > xx-1 || y < 0|| y > yy - 1) {
            sum++;
            return;
        }
        if (grid[x][y] == 0) {
            sum++;
            return;
        }
        if (grid[x][y] != 1) {
            return;
        }
        grid[x][y] = 2;
        hello463(grid, x - 1, y);
        hello463(grid, x + 1, y);
        hello463(grid, x, y - 1);
        hello463(grid, x, y + 1);
    }

}
